<?php
define('PUN_ROOT', './');
require PUN_ROOT.'include/common.php';

if ($pun_user['g_read_board'] == '0')
	message($lang_common['No view']);
	
require PUN_ROOT.'lang/'.$pun_user['language'].'/puntoolbar.php';

if ($pun_config['o_smilies'] == '1')
{
	// Display the smiley set
	if (file_exists(PUN_ROOT.'cache/cache_smilies.php')) {
		require PUN_ROOT.'cache/cache_smilies.php';
	} else {
		require_once PUN_ROOT.'include/cache_smilies.php';
		generate_smiley_cache();
		require PUN_ROOT.'cache/cache_smilies.php';
	}
	
	// Most of the loop was taken from Rickards mod_easy_bbcode.php
	$smiley_dups = array();
	$num_smilies = count($smiley_text);
	
	for ($i = 0; $i < $num_smilies; ++$i)
	{
		// Is there a smiley at the current index?
		if (!isset($smiley_text[$i]))
			continue;
	
		if (in_array($smiley_img[$i], $smiley_dups))
		{
				// Unset duplicate entries
				unset($smiley_text[$i]);
				unset($smiley_img[$i]);
		}
		else
			$smiley_dups[] = $smiley_img[$i];
	}
	
	// Re-index the arrays
	$smiley_text = array_values($smiley_text);
	$smiley_img = array_values($smiley_img);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="<?php echo $lang_common['lang_direction']; ?>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $lang_common['lang_encoding']; ?>" />
<title><?php echo $lang_ptb['Smilies']; ?></title>
<link rel="stylesheet" type="text/css" href="style/<?php echo $pun_user['style'].'.css' ?>" />
<script type="text/javascript">
//<![CDATA[
	var textarea = window.opener.document.getElementById('req_message');

	var smilies = document.createElement("div");
	smilies.id = "smilies";

	var smiliesTxt = new Array();
	var smiliesImg = new Array();

	<?php
	$stop = count($smiley_text);
	for ($i = 0; $i < $stop; ++$i)
	{
			echo "\t".'smiliesTxt['.$i.'] = "'.stripslashes($smiley_text[$i]).'";'."\n";
			echo "\t".'smiliesImg['.$i.'] = "'.$smiley_img[$i].'";'."\n";
	}
	?>

	function addSmiley(src, txt)
	{
		var i = document.createElement('img');
		var htxt = txt;
		htxt = htxt.replace(new RegExp(/&amp;/g),'&');
		htxt = htxt.replace(new RegExp(/&quot;/g),'"');
		htxt = htxt.replace(new RegExp(/&lt;/g),'<');
		htxt = htxt.replace(new RegExp(/&gt;/g),'>'); 
		i.src = src;
		i.title = htxt;
		i.style.padding = '0.2em';
		i.onclick = function() { try { insertSmiley(htxt) } catch (e) { } return false };
		i.tabIndex = 400;
		smilies.appendChild(i);
	}

	function insertSmiley(txt)
	{
		encloseSelection(txt, '');
		window.close();
	}

	function encloseSelection(prefix, suffix, fn)
	{
		textarea.focus();
		var start, end, sel, scrollPos, subst;

		if (typeof(window.opener.document["selection"]) != "undefined") {
			sel = window.opener.document.selection.createRange().text;
		} else if (typeof(textarea["setSelectionRange"]) != "undefined") {
			start = textarea.selectionStart;
			end = textarea.selectionEnd;
			scrollPos = textarea.scrollTop;
			sel = textarea.value.substring(start, end);
		}

		if (sel.match(/ $/)) { // exclude ending space char, if any
			sel = sel.substring(0, sel.length - 1);
			suffix = suffix + " ";
		}

		if (typeof(fn) == 'function') {
			var res = (sel) ? fn(sel) : fn('');
		} else {
			var res = (sel) ? sel : '';
		}

		subst = prefix + res + suffix;

		if (typeof(window.opener.document["selection"]) != "undefined") {
			var range = window.opener.document.selection.createRange().text = subst;
			textarea.caretPos -= suffix.length;
		} else if (typeof(textarea["setSelectionRange"]) != "undefined") {
			textarea.value = textarea.value.substring(0, start) + subst +
			textarea.value.substring(end);
			if (sel) {
				textarea.setSelectionRange(start + subst.length, start + subst.length);
			} else {
				textarea.setSelectionRange(start + prefix.length, start + prefix.length);
			}
			textarea.scrollTop = scrollPos;
		}
	}
//]]>
</script>
</head>

<body>

<div class="block" style="margin:0.5em">
	<div class="box">
		<div id="smilies_box" class="inbox">

		<script type="text/javascript">
		//<![CDATA[
			for (var i=0; i<smiliesTxt.length; i++)
			{
				addSmiley('img/smilies/'+smiliesImg[i],smiliesTxt[i]);
			}
			document.getElementById('smilies_box').appendChild(smilies);
		//]]>
		</script>

		</div>
	</div>
</div>

</body>
</html>

<?php
}
else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="<?php echo $lang_common['lang_direction']; ?>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $lang_common['lang_encoding']; ?>" />
<title><?php echo $lang_common['Smilies']; ?></title>
<link rel="stylesheet" type="text/css" href="style/<?php echo $pun_user['style'].'.css' ?>" />
</head>

<body>

<div class="block" style="margin:0.5em">
	<div class="box">
		<div id="smilies_box" class="inbox">

		<p><?php echo $lang_common['Smilies'].': '.$lang_common['off']; ?></p>

		</div>
	</div>
</div>

</body>
</html>

<?php
} ?>